<template>
	<basic-container>
		<avue-crud :option="option" :data="data" ref="crud" v-model="form" :table-loading="loading" :permission="permissionList"
		@row-del="rowDel" @row-update="rowUpdate" @row-save="rowSave" :page="page" @selection-change="selectionChange"
		@refresh-change="refreshChange" @search-change="searchChange" @search-reset="searchReset" @current-change="currentChange"
		@size-change="sizeChange" @on-load="onLoad">
			<template slot="menuLeft">
				<!--<el-button type="primary" size="mini" @click="delAll()">批量删除</el-button>-->
			</template>
		</avue-crud>
	</basic-container>
</template>

<script>
	import {
		getList,
		remove,
		update,
		add,
		deletes
	} from "@/api/base/store/storeType";
	import {
		mapGetters
	} from "vuex";
	import {bannerDescribe} from '@/util/rej';
	import  {
		validateFormat
	} from "../../../util/util";

	export default {
		data() {
			return {
				form: {},
				query: {},
				loading: true,
				rolesTree: [],
				rolesKeys: [],
				idList: [],
				box: false,
				page: {
					pageSize: 10,
					currentPage: 1,
					total: 0
				},
				option: {
					size: 'mini',
					excelBtn: false,
					tip: false,
					border: true,
					index: true,
					// selection: true,
					viewBtn: true,
					rowKey: 'id',
					height: 'auto',
					// calcHeight: 380,
					dialogType: 'dialog',
					menuWidth: 300,
					column: [{
							label: "店铺类型",
							prop: "storeType",
							fixed: true,
							maxlength:12,
							rules: [{
								required: true,
								message: "请输入店铺类型名称",
								trigger: "blur"
							},{
								validator:validateFormat,
								trigger:'blur'
							}]
						},
						{
							label: "店铺描述",
							prop: "descs",
							maxlength:50,
							rules: [{
								required: true,
								validator:bannerDescribe,
								trigger: "change"
							},{
								validator:validateFormat,
								trigger:'blur'
							}]
						}
					]
				},
				data: []
			};
		},
		computed: {
			...mapGetters(["permission", 'userInfo']),
			permissionList() {
				return {
					addBtn: this.vaildData(this.permission["/mall-cloud-base/storeType/addItem"], true),
					viewBtn: this.vaildData(this.permission["/mall-cloud-base/storeType/detail"], false),
					delBtn: this.vaildData(this.permission["/mall-cloud-base/storeType/delete"], true),
					editBtn: this.vaildData(this.permission["/mall-cloud-base/storeType/editItem"], true)
				};
			},
		},
		methods: {
			//监控多选点击事件
			selectionChange(row) {
				var idList = [];
				if (row.length > 0) {
					for (let item of row) {
						idList.push(item.id)
					}
					this.idList = idList
				}
			},
			delAll() {
				if (this.idList.length == 0) {
					this.$message.info("请先选择要删除的对象")
					return;
				}
				deletes(this.idList).then(res => {
					if (res.data.code == 200) {
						this.refreshChange();
						this.$message({
							type: "success",
							message: "删除成功!"
						});
					} else {
						this.$message.error(res.data.message);
					}
				})
			},
			rowSave(row, loading, done) {
				row.position = row.positionIds;
				row.deptName = row.$deptId;
				row.repassword = row.password;
				loading();
				add(row).then((res) => {
					if(res.status==200){
						this.onLoad(this.page);
						this.$message({
							type: "success",
							message: "操作成功!"
						});
						done()
					}
				}, error => {
					done();
				});
			},
			rowUpdate(row, index, loading, done) {
				row.position = row.positionIds;
				loading();
				update(row).then((res) => {
					if(res.status==200){
						this.onLoad(this.page);
						this.$message({
							type: "success",
							message: "操作成功!"
						});
						done()
					}
				}, error => {
					done();
				});
			},
			rowDel(row) {
				console.log(row)
				this.$confirm("确定将选择数据删除?", {
						confirmButtonText: "确定",
						cancelButtonText: "取消",
						type: "warning"
					})
					.then(() => {
						return remove(row.id);
					})
					.then(res => {
					    if(res.data.success){
                            this.page.currentPage = 1;
                            this.onLoad(this.page);
                            this.$message({
                                type: "success",
                                message: "操作成功!"
                            });
						}
						else{
                            this.$message.error(res.data.message);
						}
					});
			},
			searchReset() {
				this.idList = []
				this.$refs.crud.toggleSelection();
				this.query = {};
				this.onLoad(this.page);
			},
			searchChange(params,done) {
				this.page.currentPage=1
				this.idList = []
				this.$refs.crud.toggleSelection();
				this.query = params;
				this.onLoad(this.page, params,done);
			},
			currentChange(currentPage) {
				this.page.currentPage = currentPage;
			},
			sizeChange(pageSize) {
				this.page.pageSize = pageSize;
			},
			refreshChange() {
				this.idList = []
				this.$refs.crud.toggleSelection();
				this.onLoad(this.page);
			},
			onLoad(page, params = {},done) {
				this.loading = true;
				getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
					if(done){
						done();
					}
					this.loading = false;
					const data = res.data;
					this.page.total = data.count;
					this.data = data.data;
				});
			}
		}
	};
</script>

<style>
</style>
